let page = 1, rows = 5;
let pages;
$(function () {
    page = 1;
    rows = 5;
    $("#btnSearch").click(search);
    $('#nextPage').click(nextPage);
    $('#previousPage').click(previousPage);
    $('#gotoPage').click(gotoPage);
    $('#firstPage').click(firstPage);
    $('#lastPage').click(lastPage);
});
function lastPage() {
    page = pages;
    search()
}
function firstPage() {
    page = 1;
    search();
}

function nextPage() {
    if(page === pages){
        return;
    }
    page++;
    search();
}

function previousPage() {
    if (page <= 1) {
        return;
    }
    page--;
    search();
}

function gotoPage() {
    let text = $('#gotopage_text').val();
    if (isNaN(parseInt(text, 10))) {
        return;
    }
    page = parseInt(text, 10);
    if(page < 1){
        page = 1;
    }else if(page > pages){
        page = pages;
    }
    search();
}

function search() {
    var val = $("#keyword").val().trim();  // 去掉无效空格
    if (val.length === 0) {
        alert("请输入小说的名字或者作者的名字...");
        return;
    }
    $.ajax({
        url: "./novelSearch.do",
        type: "post",
        dataType: "json",
        data: {
            "keyword": val,
            'page': page,
            'rows': rows
        },
        error: function (data) {
            alert(data.status);
        },
        success: function (result) {
            $("#list").html("");
            $(result.data).each(function (index, novel) {
                var $tr = $("<tr><td>" + ((page - 1) * rows + index + 1) + "</td><td><a href='./chapterList.do?url=" + novel.url + "'>"
                    + novel.name + "</a></td><td>"
                    + novel.author + "</td><td>"
                    + novel.lastUpdateChapter + "</td><td>"
                    + (novel.status === 1 ? "连载" : "完结") + "</td><td>"
                    + getPlatform(novel.platformId) + "</td></tr>");
//                    console.log($tr);
                $("#list").append($tr);
                pages = result.pages;
                $('#currentPage').html( page + '/' + pages );
            });
        }
    });
}

function getPlatform(platformId) {
    if (platformId === 1) {
        return "顶点小说";
    } else if (platformId === 2) {
        return "笔趣阁";
    } else if (platformId === 3) {
        return "笔下文学";
    } else if (platformId === 4) {
        return "看书中";
    } else {
        return "未知";
    }
}

//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;
    return scrollTop;
}


//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}

//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;
}

window.onscroll = function () {
    //监听事件内容
    if(getScrollHeight() === getWindowHeight() + getDocumentTop()){
       alert('触发事件');
    }
};